package com.beta.restapi.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.web.ContentType;
import com.beta.dto.order.OrdersDto;
import com.beta.restapi.internal.Consts;
import com.beta.service.ISalesService;
import com.beta.vo.sales.SaleOrderVO;
import com.beta.vo.sales.SalesOrderPageVO;
import io.swagger.annotations.Api;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by lichao on 2016/11/29.
 */
@Api(tags = "SalesOrder")
@RestController
@RequestMapping(Consts.API_PATH_PREFIX + Consts.Resource.SALES_ORDERS)
public class SalesOrdersController extends BaseController {

    @Autowired
    private MapperFacade beanMapper;
    @Autowired
    private ISalesService salesService;

    @GetMapping(value = {"assigning", "assigning.json"}, produces = ContentType.JSON)
    public Object getAssigningOrders(@RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        Page<OrdersDto> assigningSalesOrders = salesService.getAssigningSalesOrders(getCurrentUserId(), getMybatisPage());
        return ok(toSaleOrderPageVo(assigningSalesOrders));
    }

    @GetMapping(value = {"delivering", "delivering.json"}, produces = ContentType.JSON)
    public Object getDeliveringOrders(@RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        Page<OrdersDto> deliveringSalesOrders = salesService.getDeliveringSalesOrders(getCurrentUserId(), getMybatisPage());
        return ok(toSaleOrderPageVo(deliveringSalesOrders));
    }

    @GetMapping(value = {"complete", "complete.json"}, produces = ContentType.JSON)
    public Object getCompleteOrders(@RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        Page<OrdersDto> completeSalesOrders = salesService.getCompleteSalesOrders(getCurrentUserId(), getMybatisPage());
        return ok(toSaleOrderPageVo(completeSalesOrders));
    }

    private Object toSaleOrderPageVo(Page<OrdersDto> ordersDtoPage) {
        List<OrdersDto> dtoRows = ordersDtoPage.getRecords();
        SalesOrderPageVO salesOderPageVO = new SalesOrderPageVO();
        salesOderPageVO.setOrders(beanMapper.mapAsList(dtoRows, SaleOrderVO.class));
        fillPageParam(ordersDtoPage, salesOderPageVO);
        return salesOderPageVO;
    }

}
